package com.zjw.zy.coreui.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.SeekBar
import android.widget.TextView
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.R
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/29 16:12
 */
class RadioProgressView : BaseViewGroup {

    private val mProgressBar: SeekBar by lazy { findViewById(R.id.radio_progress_view_progress) }
    private val mPlayButton: ImageView by lazy { findViewById(R.id.radio_progress_option) }
    private val mCurrentText: TextView by lazy { findViewById(R.id.radio_progress_current) }
    private val mTotalText: TextView by lazy { findViewById(R.id.radio_progress_total) }


    private var mListener: Listener? = null

    private var isPlay = false

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.radio_progress_view
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindListener() {

        mProgressBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    mListener?.onProgressChange(progress, mProgressBar.max)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                mListener?.onProgressStartChange()
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                mListener?.onProgressEndChange()
            }
        })

        mPlayButton.setOnClickListener {
            if (isPlay) {
                mPlayButton.background = ResourcesUtils.getDrawable(R.drawable.icon_play)
                mListener?.onPause()
            } else {
                mPlayButton.background = ResourcesUtils.getDrawable(R.drawable.icon_pause)
                mListener?.onPlay()
            }
            isPlay = !isPlay
        }
    }

    fun isPlay() = isPlay

    override fun onBindValue() {
        isPlay = true
        mPlayButton.background = ResourcesUtils.getDrawable(R.drawable.icon_pause)
    }

    override fun onFindView(rootView: View) {

    }

    fun updateMax(max: Int) {
        mProgressBar.max = max

        mTotalText.text = TimeUtils.parseToStrTime(max)
    }

    fun updateProgress(progress: Int) {
        mProgressBar.setProgress(progress, true)

        mCurrentText.text = TimeUtils.parseToStrTime(progress)
    }


    fun bindListener(listener: Listener) {
        mListener = listener
    }


    interface Listener {
        fun onPlay()

        fun onPause()

        fun onProgressChange(position: Int, total: Int)

        fun onProgressStartChange()

        fun onProgressEndChange()
    }
}